import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/home'
    },{
        path: '/home',
        name: 'home',
        component: () => import( /* webpackChunkName: "home" */ '../views/Home/index.vue')
    },{
        path: '/login',
        name: 'login',
        component: () => import( /* webpackChunkName: "login" */ '../views/Login')
    },{
        path: '/set',
        name: 'set',
        component: () => import( /* webpackChunkName: "set" */ '../views/Set')
    },{
        path: '/in-store',
        name: 'in-store',
        component: () => import( /* webpackChunkName: "in-store" */ '../views/in-store')
    },{
        path: '/out-store',
        name: 'out-store',
        component: () => import( /* webpackChunkName: "out-store" */ '../views/out-store')
    },{
        path: '/store-record',
        name: 'store-record',
        component: () => import( /* webpackChunkName: "store-record" */ '../views/store-record')
    }
]



const router = new VueRouter({
    routes
})

router.beforeEach((to, from, next) => {
    let token = window.sessionStorage.getItem('token');
    // 设置通过白名单 只有出库和出入库记录才需要强制登录
    let whiteList = ['/', '/login', '/home', '/in-store'];
    if (!token) {
        if (whiteList.indexOf(to.path) > -1) {
            next();
        } else {
            next({
                path: '/login',
                query: { redirect: to.fullPath }
            });
        }
    } else {
        if (to.path == '/login') {
            next('/home');
        } else {
            next();
        }
        
    }
    
})

export default router